package com.t216.dm216.controller;

import cn.hutool.core.bean.BeanUtil;
import com.t216.dm216.common.Result;
import com.t216.dm216.common.UserMap;
import com.t216.dm216.pojo.DmLinkUser;
import com.t216.dm216.pojo.DmUser;
import com.t216.dm216.pojo.dto.DmUserDTO;
import com.t216.dm216.service.DmLinkUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user/api/v")
public class DmUserApiViewController {
    @Resource
    private DmLinkUserService dmLinkUserService;
    @PostMapping("/queryLinkUser")
    public Result queryLinkUser(){
        DmUserDTO user = UserMap.get();
        List<DmLinkUser> list = dmLinkUserService.getLinkUsersByUserId(user.getUserId());
        list.forEach(System.out::println);
        return Result.success(list);
    }
    @PostMapping("/queryLinkUserByIdCard")
    public Result queryLinkUserByIdCard(@RequestBody DmLinkUser linkUser){
        DmUserDTO user = UserMap.get();
        linkUser.setUserId(user.getUserId());

        return dmLinkUserService.getCountByIdCardAndUserId(linkUser) == 1 ? Result.error("1020") : Result.success();
    }

    @PostMapping("/addLinkUser")
    public Result addLinkUser(@RequestBody DmLinkUser linkUser){
        DmUserDTO user = UserMap.get();
        linkUser.setCreatedTime(new Date());
        linkUser.setUserId(user.getUserId());

        return dmLinkUserService.addLinkUser(linkUser) ? Result.success() : Result.error("1020");
    }
}
